import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class Coordinate {

  //第一次就定下的参数，写成final
  final Color axisColor;//轴线颜色
  final double strokeWidth;//轴线宽度
  final int xScaleCount;
  final int yScaleCount;

  //后期可以考虑动态设置或用户自定义，所以不写成final
  double originPointX;
  double originPointY;
  int pointCount;// 绘图原点数目上限
  int xPointGap;
  int yPointGap;



  Coordinate({
    this.axisColor = Colors.black,//轴线颜色
    this.strokeWidth = 1,//轴线宽度
    this.xScaleCount = 10,//x轴间隔刻度总单位
    this.yScaleCount = 10,//y轴间隔刻度总单位

    this.originPointX = 0,
    this.originPointY = 0,
    this.pointCount = 100,//点数量
    this.xPointGap = 10,//x轴显示刻度最大数目
    this.yPointGap = 10,//y轴显示刻度最大数目



  }){
    axisPaint
      ..color = axisColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = strokeWidth;


  }

  final TextPainter textPainter = TextPainter(
    textAlign: TextAlign.center,
    textDirection: TextDirection.ltr,
  );


  final Paint axisPaint = Paint();
  final Paint gridAxisPaint = Paint()..style=PaintingStyle.stroke..color=Colors.grey..strokeWidth=0.5;//网格线形，颜色，粗细



  void paint(Canvas canvas,size){
    _drawAxis(canvas,size);
    int StartStep = 1 ;
    //绘制X轴文字
    double stepX = size.width/(2*xScaleCount);//x轴步长
    // print(size.width/xScaleCount);
    for(int i = StartStep;i<xScaleCount*2;i++){
      String value = ((i-xScaleCount - originPointX)).toStringAsFixed(1);
      // print(value);
      if (value == (0.0-originPointX).toStringAsFixed(1)) { // 防止 0.0 的字样与0字样重合
        value = "";
      }
      textPainter.text =  TextSpan(
          text: value,
          style: const TextStyle(fontSize: 10, color: Colors.black));
      textPainter.layout(); // 进行布局
      textPainter.paint(
          canvas,
          Offset(i*stepX-textPainter.size.width/2,-size.height/2));//文字偏移
      canvas.drawLine(Offset(i*stepX,-size.height/2),Offset(i*stepX,-size.height/2 - 5),axisPaint);//刻度线
      canvas.drawLine(Offset(i*stepX,0),Offset(i*stepX,-size.height),gridAxisPaint);//网格线

    }
    //绘制Y轴文字
    double stepY = size.height/(2*yScaleCount);//y轴步长

    for(int i = StartStep;i<yScaleCount*2;i++){
      String value = ((i-yScaleCount - originPointY)).toStringAsFixed(1);
      if (value == (0.0-originPointY).toStringAsFixed(1)) {// 防止 0.0 的字样与0字样重合
        value = "";
      }

      textPainter.text =  TextSpan(
          text: value, style: const TextStyle(fontSize: 10, color: Colors.black));
      textPainter.layout(); // 进行布局
      textPainter.paint(
          canvas,
          Offset(-textPainter.size.width + size.width/2 - 5,- (i*stepY+textPainter.size.height/2)));//文字偏移
      canvas.drawLine(Offset(size.width/2,-i*stepY),Offset(size.width/2 + 5,-i*stepY),axisPaint);//刻度线
      canvas.drawLine(Offset(0,-i*stepY),Offset(size.width,-i*stepY),gridAxisPaint);//网格线
    }
    //绘制0
      textPainter.text =  TextSpan(
          text: '', style: const TextStyle(fontSize: 12, color: Colors.black));
      textPainter.layout(); // 进行布局
      textPainter.paint(canvas, Offset(size.width/2 - 10 ,-size.height/2));
  }
  void _drawAxis(Canvas canvas,Size size){

    final double halfWidth = size.width / 2;
    final double halfHeight = size.height / 2;

    final Offset yAxisStart = Offset(halfWidth, -halfHeight*2);
    final Offset xAxisStart = Offset(halfHeight*2, -halfWidth);

    final double arrowLength = 5; // 箭头长度

// 画y轴线
    canvas.drawLine(yAxisStart, Offset(halfWidth, 0), axisPaint);

// 画x轴线
    canvas.drawLine(Offset(0, -halfWidth), Offset(size.height, -halfWidth), axisPaint);

// 画y轴箭头
    canvas.drawLine(yAxisStart, Offset(halfWidth + arrowLength, -(halfHeight*2 - arrowLength)), axisPaint);

    canvas.drawLine(yAxisStart, Offset(halfWidth - arrowLength, -(halfHeight*2 - arrowLength)), axisPaint);

// 画x轴箭头
    canvas.drawLine(xAxisStart, Offset(size.height - arrowLength, -(halfWidth + arrowLength)), axisPaint);
    canvas.drawLine(xAxisStart, Offset(size.height - arrowLength, -(halfWidth - arrowLength)), axisPaint);

    // canvas.drawLine(Offset(size.width/2, -size.height), Offset(size.width/2, 0), axisPaint);//y轴线
       // canvas.drawLine(Offset(0, -size.width/2), Offset(size.height, -size.width/2), axisPaint);//x轴线
       //
       // canvas.drawLine(Offset(size.width/2, -size.height), Offset(195, -390), axisPaint);//y轴箭头左
       // canvas.drawLine(Offset(size.width/2, -size.height), Offset(205, -390), axisPaint);//y轴箭头右
       //
       // canvas.drawLine(Offset(size.height, -size.width/2), Offset(390, -195), axisPaint);//x轴箭头下
       // canvas.drawLine(Offset(size.height, -size.width/2), Offset(390, -205), axisPaint);//x轴箭头上

    // Path axisPath = Path();
    //
    // // axisPath.relativeLineTo(size.width, 0);//x轴
    // axisPath.relativeLineTo(-10, -4);//X轴箭头上半部分
    // axisPath.moveTo(size.width, 0);//x轴箭头上半部分偏移
    // axisPath.relativeLineTo(-10, 4);//X轴箭头下半部分
    // axisPath.moveTo(0, 0);//y轴偏移
    // // axisPath.relativeLineTo(0, -size.height);//y轴
    // axisPath.relativeLineTo(-4, 10);//y轴箭头左半部分
    // axisPath.moveTo(0, -size.height);//y轴箭头偏移
    // axisPath.relativeLineTo(4, 10);//y轴箭头右半部分
    // canvas.drawPath(axisPath, axisPaint);


    textPainter.text = const TextSpan(
      text:'X轴',style: TextStyle(fontSize: 12,color: Colors.black));
    textPainter.layout(); // 进行布局
    Size textSize = textPainter.size; // 尺寸必须在布局后获取
    textPainter.paint(canvas, Offset(size.width - textSize.width, -size.height/2 + textSize.height/2));

    textPainter.text = const TextSpan(
        text: 'y 轴', style: TextStyle(fontSize: 12, color: Colors.black));
    textPainter.layout(); // 进行布局
    Size textSize2 = textPainter.size; // 尺寸必须在布局后获取
    textPainter.paint(canvas,
        Offset(size.width/2 - textSize2.width/2, -size.height - textSize2.height-3));
  }
}